home *** CD-ROM | disk | FTP | other *** search
/ DOpus Plus / DOpus Plus.iso / Tutorial / C Guide / Code_Fragments / LaunchIPC.c < prev    next >
C/C++ Source or Header  |  1998-09-17  |  4KB  |  117 lines

  1. /*******************************************************************
  2.  
  3.   LaunchIPC.c
  4.   
  5.   Makes launching your own IPC_process so easy like possible
  6.   (are we not all lazy ... ? ;) ). You does not need to declare
  7.   any functions "__saveds" and/or "__asm" to launch a new process,
  8.   this is all here done.
  9.   
  10.   You must have a global memorypool called "mempool" to use this
  11.   like it is (, else you must change it a little bit...).
  12.   
  13.   The only thing you have to do, is to copy the prototype of
  14.   LaunchIPC() ...
  15.     
  16. *********************************************************************/  
  17.   
  18. #include "includes/LaunchIPC.h" 
  19.  
  20. /********************************************************************/
  21. // this you should declare in your other files
  22.  
  23. extern BOOL __saveds LaunchIPC( void (*func_entry)(), APTR data, IPCData **saveto, STRPTR proc_name, ULONG stack );
  24.  
  25. // arguments: 
  26. // func_entry - your new process entry point...
  27. //              your function must be the type "void name( IPCData *ipc, APTR data )"
  28. //              and does reveive also its own IPC pointer as argument
  29. // data       - any data you want to have as argument for your new process
  30. //              but keep in mind that this data remains valid long enough...
  31. // saveto     - a pointer to save the IPCData of the new process (must be supplied !) 
  32. // proc_name  - name of the process name to create
  33. // stack      - stack size to use for the new process (if NULL -> default 4096)
  34.  
  35. // the function does return TRUE on success
  36.  
  37. /********************************************************************/
  38. // local prototypes
  39.  
  40. void __saveds Func_Startup( void );
  41.  
  42. ULONG __asm __saveds startup_startup( register __a0 IPCData   *ipc,
  43.                                       register __a1 StartupData *cd );
  44.                                                                                                  
  45. /********************************************************************/
  46. // all stuff is really simple, but it may save a lot of work,
  47. // especially if you need more than one process.  
  48.  
  49. // this function does allocate some memory and launches the new process
  50.  
  51. BOOL __saveds LaunchIPC( void (*func_entry)(), APTR data, IPCData **saveto, STRPTR proc_name, ULONG stack )
  52. {
  53.      StartupData *sd;
  54.           
  55.      if( (sd = AllocMemH( mempool, sizeof(StartupData))) )
  56.        {                   
  57.           sd->a4      =                    getreg( REG_A4 );
  58.           sd->module  = (struct Library *) getreg( REG_A6 );
  59.           sd->library = DOpusBase;
  60.           sd->entry   = func_entry;
  61.           sd->data    = data;
  62.                          
  63.           if( !stack )
  64.                stack = 4096;
  65.                                                                      
  66.           IPC_Launch( 0, saveto, proc_name,
  67.                       (ULONG) Func_Startup, stack,
  68.                       (ULONG) sd, DOSBase );
  69.                                                          
  70.           if( saveto )
  71.                return TRUE;
  72.                                 
  73.           FreeMemH( sd );                                                      
  74.        }
  75.                  
  76.      return FALSE;
  77. }
  78.  
  79. void __saveds Func_Startup( void )
  80. {
  81.      struct Library *DOpusBase;
  82.      IPCData        *ipc;
  83.      StartupData    *sd;
  84.      
  85.      if (!(DOpusBase=(struct Library *)FindName(&((struct ExecBase *)*((ULONG *)4))->LibList,"dopus5.library")))
  86.          return;
  87.  
  88.      ipc = IPC_ProcStartup( (ULONG *) &sd, startup_startup);
  89.  
  90.      putreg( REG_A4, sd->a4 );
  91.  
  92.      if( ipc )
  93.        {
  94.           sd->module->lib_OpenCnt++;
  95.                          
  96.           sd->entry( ipc, sd->data );  // starting your function
  97.                          
  98.           sd->module->lib_OpenCnt--;
  99.        }
  100.                  
  101.      IPC_Free( sd->ipc );
  102.      FreeMemH( sd ); 
  103.      return;
  104. }
  105.  
  106. ULONG __asm __saveds startup_startup( register __a0 IPCData   *ipc,
  107.                                       register __a1 StartupData *sd )
  108. {
  109.      struct Library *DOpusBase;
  110.         
  111.      putreg( REG_A4, sd->a4 );
  112.      sd->ipc = ipc;
  113.      DOpusBase = sd->library;  // does produce a warning here...
  114.                   
  115.      return TRUE;
  116. }
  117.